package com.moody.init;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moody.hibernate.domain.CodeLib;

/*
 * Copyright © Moody analytics
 *
 * @Author: Yan Jian
 * @Time: 2011-10-24 下午01:50:26
 * @Modified by: 
 * @Description: 
 */
public class Collateral {
	private static final Logger log = LoggerFactory
	.getLogger(DataDictionary.class);
	private static Collateral instance = null;
	
	private Collateral() {

	}

	public static Collateral getInstance() {
		if (instance == null) {
			instance = new Collateral();
		}
		return instance;
	}
	
	public List getFirstLevel(){
		
		Map<String, CodeLib> collateralType = new LinkedHashMap<String, CodeLib>();
		collateralType = DataDictionary.getInstance().getDataDictionary().get("GuarantyType");
		
		List list = new ArrayList();
		
		Map<String, String> map1 = new LinkedHashMap<String, String>();
		map1.put("code", "0401");
		map1.put("name", collateralType.get("0401").getItemName());
		list.add(map1);
		Map<String, String> map2 = new LinkedHashMap<String, String>();
		map2.put("code", "0402");
		map2.put("name", collateralType.get("0402").getItemName());
		list.add(map2);
		Map<String, String> map3 = new LinkedHashMap<String, String>();
		map3.put("code", "0403");
		map3.put("name", collateralType.get("0403").getItemName());
		list.add(map3);
		Map<String, String> map4 = new LinkedHashMap<String, String>();
		map4.put("code", "0404");
		map4.put("name", collateralType.get("0404").getItemName());
		list.add(map4);
		
		return list;
	}
	
	public Map getSecondLevel(){
		Map<String, CodeLib> collateralType = new LinkedHashMap<String, CodeLib>();
		collateralType = DataDictionary.getInstance().getDataDictionary().get("GuarantyType");
		
		Map map = new LinkedHashMap();
		Iterator iter = collateralType.entrySet().iterator(); 
		
		while (iter.hasNext()) { 
			Map.Entry entry = (Map.Entry) iter.next(); 
			String collateralCode = (String) entry.getKey(); 
		    String collateralName = ((CodeLib)entry.getValue()).getItemName();
		    
		    if(collateralCode.length() == 6 || collateralCode.length() == 8){
		    	String firstLevelCode = collateralCode.substring(0, 4);
		    	if(!map.containsKey(firstLevelCode)){
		    		List<Map<String, String>> secondLevel = new ArrayList<Map<String, String>>();
		    		Map<String, String> secondLevelMap = new LinkedHashMap<String, String>();
		    		secondLevelMap.put("code", collateralCode);
		    		secondLevelMap.put("name", collateralName);
		    		secondLevel.add(secondLevelMap);
			    	map.put(firstLevelCode, secondLevel);
		    	}
		    	else{
		    		Map<String, String> secondLevelMap = new LinkedHashMap<String, String>();
		    		secondLevelMap.put("code", collateralCode);
		    		secondLevelMap.put("name", collateralName);
		    		((List)map.get(firstLevelCode)).add(secondLevelMap);
		    	}
		    }
			
		}
		
		return map;
		
	}
	

}
